package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.INewsTypeDao;
import com.cn.wanxi.model.NewsTypeModel;
import com.cn.wanxi.util.GetSqlSession;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class NewsTypeDaoImpl implements INewsTypeDao {
    private void clearNewsTypeRedis(){
        try {
            Jedis jedis = new Jedis();
            jedis.del("news_type_id");
            jedis.del("news_type_name");
            jedis.del("news_type_href");
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    @Override
    public NewsTypeModel getNewsType(int id) {
        return GetSqlSession.getSqlSession().selectOne("com.cn.wanxi.dao.INewsTypeDao.getNewsType",id);
    }

    @Override
    public int addNewsType(NewsTypeModel newsType) {
        clearNewsTypeRedis();
        return GetSqlSession.getSqlSession().insert("com.cn.wanxi.dao.INewsTypeDao.addNewsType",newsType);
    }

    @Override
    public int editNewsType(NewsTypeModel newsType) {
        clearNewsTypeRedis();
        return GetSqlSession.getSqlSession().update("com.cn.wanxi.dao.INewsTypeDao.editNewsType",newsType);
    }

    @Override
    public int deleteNewsType(int id) {
        clearNewsTypeRedis();
        return GetSqlSession.getSqlSession().update("com.cn.wanxi.dao.INewsTypeDao.deleteNewsType",id);
    }

    @Override
    public List<NewsTypeModel> getNewsTypeList() {
        return GetSqlSession.getSqlSession().selectList("com.cn.wanxi.dao.INewsTypeDao.getNewsTypeList");
    }

    @Override
    public List<NewsTypeModel> getNewsTypes(Map<String,Object> map) {
        return GetSqlSession.getSqlSession().selectList("com.cn.wanxi.dao.INewsTypeDao.getNewsTypes",map);
    }

    @Override
    public long getNewsTypeCount(Map<String, Object> map) {
        return GetSqlSession.getSqlSession().selectOne("com.cn.wanxi.dao.INewsTypeDao.getNewsTypeCount",map);
    }

    @Override
    public int updateShow(NewsTypeModel newsType) {
        clearNewsTypeRedis();
        return GetSqlSession.getSqlSession().update("com.cn.wanxi.dao.INewsTypeDao.updateShow",newsType);
    }

    @Override
    public List<NewsTypeModel> getNewsTypeListByRedis() {
        Jedis jedis = new Jedis();
        List<NewsTypeModel> list = new ArrayList<>();
        List<String> newsTypeId = jedis.lrange("news_type_id",0,-1);
        for (int i = 0;i<newsTypeId.size();i++){
            NewsTypeModel newsType = new NewsTypeModel();
            newsType.setId(Integer.valueOf(newsTypeId.get(i)));
            newsType.setName(jedis.lindex("news_type_name",i));
            newsType.setHref(jedis.lindex("news_type_href",i));
            list.add(newsType);
        }
        return list;
    }

    @Override
    public void setNewsTypeListInRedis(List<NewsTypeModel> list) {
        Jedis jedis = new Jedis();
        for(NewsTypeModel newsType:list){
            jedis.rpush("news_type_id", String.valueOf(newsType.getId()));
            jedis.rpush("news_type_name",newsType.getName());
            jedis.rpush("news_type_href",newsType.getHref());
        }
    }
}
